#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

struct ListNode {
      int val;
      ListNode *next;
      ListNode() : val(0), next(nullptr) {}
      ListNode(int x) : val(x), next(nullptr) {}
      ListNode(int x, ListNode *next) : val(x), next(next) {}
  };

class Solution {
public:
    ListNode* trainningPlan(ListNode* l1, ListNode* l2)
    {
        if (l1 == NULL)
            return l2;
        if (l2 == NULL)
            return l1;
        ListNode* newhead = NULL;
        ListNode* newtail = NULL;

        ListNode* cur1 = l1;
        ListNode* cur2 = l2;
        while (cur1 != NULL && cur2 != NULL)
        {
            if (cur1->val < cur2->val)
            {
                if (newhead == NULL)
                {
                    newhead = newtail = cur1;
                }
                else
                {
                    newtail->next = cur1;
                    newtail = newtail->next;
                }
                cur1 = cur1->next;
            }
            else
            {
                if (newhead == NULL)
                {
                    newhead = newtail = cur2;
                }
                else
                {
                    newtail->next = cur2;
                    newtail = newtail->next;
                }
                cur2 = cur2->next;
            }
            if (cur1 == NULL)
                newtail->next = cur2;
            if (cur2 == NULL)
                newtail->next = cur1;
        }
        return  newhead;
    }
};
